package org.jeecg.modules.wx.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.modules.wx.entity.WxAccount;
import org.jeecg.common.modules.wx.service.IWxAccountService;
import org.jeecg.modules.wx.account.service.AdminWxAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: wx_account
 * @Author: jeecg-boot
 * @Date: 2020-08-08
 * @Version: V1.0
 */
@Api(tags = "wx_account")
@RestController
@RequestMapping("/wx/wxAccount")
@Slf4j
public class WxAccountController extends JeecgController<WxAccount, IWxAccountService> {
    @Autowired
    private IWxAccountService wxAccountService;
    @Resource
    private AdminWxAccountService adminWxAccountService;

    /**
     * 分页列表查询
     *
     * @param wxAccount
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "wx_account-分页列表查询")
    @ApiOperation(value = "wx_account-分页列表查询", notes = "wx_account-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WxAccount wxAccount,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<WxAccount> queryWrapper = QueryGenerator.initQueryWrapper(wxAccount, req.getParameterMap());
        Page<WxAccount> page = new Page<WxAccount>(pageNo, pageSize);
        IPage<WxAccount> pageList = wxAccountService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param wxAccount
     * @return
     */
    @AutoLog(value = "wx_account-添加")
    @ApiOperation(value = "wx_account-添加", notes = "wx_account-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WxAccount wxAccount) {
        wxAccountService.save(wxAccount);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wxAccount
     * @return
     */
    @AutoLog(value = "wx_account-编辑")
    @ApiOperation(value = "wx_account-编辑", notes = "wx_account-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WxAccount wxAccount) {
        wxAccountService.updateById(wxAccount);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "wx_account-通过id删除")
    @ApiOperation(value = "wx_account-通过id删除", notes = "wx_account-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wxAccountService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "wx_account-批量删除")
    @ApiOperation(value = "wx_account-批量删除", notes = "wx_account-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wxAccountService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "wx_account-通过id查询")
    @ApiOperation(value = "wx_account-通过id查询", notes = "wx_account-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        WxAccount wxAccount = wxAccountService.getById(id);
        if (wxAccount == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(wxAccount);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wxAccount
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WxAccount wxAccount) {
        return super.exportXls(request, wxAccount, WxAccount.class, "wx_account");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WxAccount.class);
    }


    @GetMapping("getAccessToken")
    public Result<?> getAccessToken(@RequestParam("accountId") String accountId){
        return adminWxAccountService.getAccessToken(accountId);
    }

    @GetMapping("getJsTicket")
    public Result<?> getJsTicket(@RequestParam("accountId") String accountId){
        return adminWxAccountService.getJsTicket(accountId);
    }

}
